<template>
    <el-dialog :model-value="localDialogVisible" :title="localTitle" :before-close="localCancelDialog" width="40%" top="10vh">
        <div class="sharetitle">已选择内容包</div>
        <el-scrollbar max-height="120px" :always="true">
            <div class="medianame">
                {{localDialogData.package.name}}({{localDialogData.package.packageid}})
            </div>
        </el-scrollbar>
        <div v-loading="loading" class="mt10">
            <!-- <div class="sharetitle">向合作企业开放写入权限</div> -->
            <corp-partner
                v-model:corpPartnerCheckList="corpPartnerCheckList"
                v-model:shareConfirmButton="shareConfirmButton"
                :ps-content="'PS:开放写入权限后合作企业可对内容包进行媒资写入、编目或自动编目操作'"
                :empty-content="'暂无可开放权限企业，请先建立企业互信关系再进行内容包权限开放'"
                ref="corpPartnerRef"
            />
            <!--<template v-if="corpPartnerList && corpPartnerList.length > 0">
                
            </template>-->
            <!--<el-checkbox-group class="corpchckgroup" v-if="corpPartnerList && corpPartnerList.length > 0" v-model="corpPartnerCheckList">
                <el-checkbox v-for="corpInfo in corpPartnerList" :label="corpInfo.corpid" :key="corpInfo.corpid">{{corpInfo.corpname}}</el-checkbox>
            </el-checkbox-group>-->
            
        </div>
        <template #footer>
            <!--<div v-if="corpPartnerList && corpPartnerList.length > 0" align="center">-->
            <div align="center" v-if="shareConfirmButton">
                <el-button type="primary" size="small" @click="handleShare">确定</el-button>
                <!-- <el-button type="primary" size="small" @click="localCancelDialog">取消</el-button> -->
            </div>
        </template>
    </el-dialog>
</template>

<script>
    import { ref, toRef } from 'vue'
    import { packagesWriteToCorp, getPackageWriteToCorp } from '@/api/corppartner/package'
    import {ElMessage, ElMessageBox} from 'element-plus'
    // import corpPartnerMixin from "@/mixins/corpPartnerMixin";
    import CorpPartner from '@/components/Common/CorpPartner/Index'
    export default {
        name: "",
        components: {
            CorpPartner
        },
        props: {
            title:{
                type: String,
                default: ''
            },
            visible: {
                type: Boolean,
                default: false
            },
            toDialogData: {
                type: Object,
                required: true
            }
        },
        emits: ['cancelDialog'],
        setup(props, context) {
            const localTitle = toRef(props, 'title')
            const localDialogVisible = toRef(props, 'visible')
            const localDialogData = toRef(props, 'toDialogData')
            const localCancelDialog = () => {
                context.emit('cancelDialog', 'PackageWrite')
            }
            const loading = ref(false)
            const shareConfirmButton = ref(false)
            const corpPartnerRef = ref(null)
            const corpPartnerCheckList = ref([])
            /* 获取互信企业---start */
            // const { corpPartnerList } = corpPartnerMixin({partnertype:1})
            
            
            getRelPartnersList(localDialogData.value.package.packageid)
            /* 获取互信企业---end */

            /* 获取已分享企业列表---start */
            async function getRelPartnersList( packageid ) {
                let result = await getPackageWriteToCorp({packageid:packageid})
                if( result.errcode == 0 ){
                    result.data.length > 0 ? Object.assign(corpPartnerCheckList.value, result.data): ''
                    if(corpPartnerRef.value){
                        corpPartnerRef.value.init()
                    }
                    /*
                    clearPageData = 1
                    corpPartnerList.value.forEach((val) => {
                        if( corpPartnerCheckList.value.indexOf(val.corpid) >= 0 ){
                            tableref.value.toggleRowSelection(val)
                        }
                    })
                    */
                }else{
                    console.log(result)
                }
            }
            /* 获取已分享企业列表---end */
            
            

            const handleShare = () => {
                /*if( corpPartnerCheckList.value.length <= 0 ){
                    ElMessage({type: 'warning', message: '请选择要分享的内容包', duration: 2000})
                    return
                }*/
                if( localDialogData.value.package == undefined || localDialogData.value.package.packageid == undefined ){
                    ElMessage({type: 'warning', message: '内容包ID为空', duration: 2000})
                    return
                }
                let packageids = []
                packageids.push(localDialogData.value.package.packageid)
                
                let query = {
                    packageids:packageids,
                    partnercorpids:corpPartnerCheckList.value
                }
                let elMessage = ''
                let elMessageTitle = ''
                if( corpPartnerCheckList.value.length <= 0 ){
                    elMessage = '确定 <strong>清除</strong> 所选内容包对合作企业的写入权限吗？'
                    elMessageTitle = '内容包清除写入权限'
                }else{
                    elMessage = '确定将内容包写入权限分配给所选的企业？'
                    elMessageTitle = '内容包分配写入权限'
                }
                ElMessageBox.confirm(elMessage, elMessageTitle, {
                    dangerouslyUseHTMLString: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    // console.log(query)
                    packagesWriteToCorp( query ).then(response => {
                        if( response.errcode == 0 ){
                            ElMessage({type: 'success', message: '写入权限分享成功', duration: 2000})
                            context.emit('cancelDialog', 'PackageWrite')
                        }else{
                            console.log(response)
                        }
                    })
                }).catch((err) => {
                    console.log(err)
                })


            }


            return {
                loading,
                shareConfirmButton,
                localTitle,
                localDialogVisible,
                localDialogData,
                localCancelDialog,
                corpPartnerRef,
                corpPartnerCheckList,
                handleShare
            }
        }
    }
</script>

<style scoped  lang="scss">
    
    .corpchckgroup{
        background: #f1f1f1;
        padding: 5px 10px;
        margin-top: 10px;
        .el-checkbox{
            display: flex;
            height: 25px;
        }
    }
    .sharetitle{font-size:16px;font-weight: 700;color: #333;margin-bottom: 5px;}
    .el-scrollbar__wrap{
        .medianame{margin-bottom: 5px;}
    }
    .mediaboxselect{
        overflow: hidden;
        height: 20px;
    }
    .mediaboxselectflow{
        overflow: auto;
        height: auto;
    }
    .clsmore{
        >a{color:#409eff;cursor: pointer;}
    }
</style>
<style lang="scss">
    .corppart{
        .el-table__cell{
            padding: 0;
        }
    }
</style>